All right.
So in this lecture I think we can continue to improve the ambience in our game by scripting the animations
for our Squidward friend here that's going to be sitting in the front of the kitchen.
We're basically going to be doing the exact same thing that we did in the lobby, where we'll have a
default animation or idle animation for our Squidward, and then we'll play a slightly different idle
animation randomly.
So make sure inside of your regular Squidward that you have uploaded all of the different idling animations
that I've saved in there, and that you've placed them inside of their respective idle animation instances.
Because then what we could do now is we could go into server script service inside of server, and we
can go ahead and go to our normal Squidward service.
Inside of here, we're going to make a reference to two services.
One is going to be server storage.
And then we're also going to have a reference to the server script service.
And then we can create our variable that will represent the service.
So normal Squidward service is equal to a table.
And then we'll return that at the end of our script here.
And now we can make a reference to our regular Squidward.
So he is inside of the workspace in the characters folder, and then we can get our regular Squidward.
We want to make a reference to his humanoid so regular Squidward dot humanoid, and then we can go ahead
and get his animator.
So animator is equal to humanoid dot animator, and then we can make a reference to his idle animation
regular.
So that's in regular Squidward, Dot, idle and M1, and then we'll collect all of the other idle animations
that he has, so we'll call idle animes regular Squidward dot idle animes.
And then we'll get all those children.
And lastly, we're also going to need a random data type.
Now I'm also going to be making a reference to our binding event that's in server storage.
So we're going to refer to our game comms event for the server scripts inside of server storage dot
events dot Bindable dot game comms.
Because we're going to be using this event for when we need to move our Squidward.
So we're going to have him later.
Walk to a point right here that's invisible point.
And then he's going to open the store, walk down through here to this second point, open this door,
and then enter into the basement.
So this is going to be the part in our story where the lights go out.
And then he's going to go check out the basement or the breaker in the basement.
And then he disappears and we have to go look for him.
So we're going to be using that event later when we need to go ahead and move our Squidward.
And because we're going to be doing that, we also need to make a reference to our game comms enum for
the server, so we can require from server script service, dot server, dot modules, dot enums, dot
game communication enum.
Now, since we're going to be doing the exact same thing that we did in our lobby, we of course need
a function for starting the idle animations.
We're going to need a function for getting a random track or idle animation track, and that means we're
actually going to have to make sure to load all of our idle animations onto our, uh, humanoid.
So for every single animation in idle animation, what we need to do is we need to get the track from
our animator load animation anim, and then we need to take that track and insert it into a table so
we can have idle anim tracks, and then we can insert this track into our idle anim tracks table.
And then we'll pass the track perfect.
And then inside of our get random track function, all we need to do is return from our Idle Anim Tracks
table.
We'll use our RNG next integer function, pass one and then the max number of idle anim tracks.
So we just get a random animation.
We also need to make sure to load our regular default idle animation right here, so we can create a
variable for that real quick.
We'll call it idle anim track is equal to the animator load animation idle anim regular.
And then from this point inside of our star idle animation function, what we could do is we could listen
for while our regular Squidward dot parent is not equal to nil.
And this is because we're going to have him move to those different positions on our map later on.
And when that happens, he'll reach this position.
Open this door, open this door, reach the second one, and then go to the third position.
And after that, we're just going to delete them off the map because we don't need them anymore.
And when he gets deleted off of the map, that means his parent is going to be set to nil.
So when his parent does get set to nil, we want to stop looping and animating him.
So in this loop, we can check if our idle animation track is not playing.
So if it's not playing then we need to play it.
So idle anim track play.
And then this idle animation track has that exact same event inside of it.
So idle anim track, we're going to get marker reach signal, which is the ended event, and we're going
to wait for this.
And when that is reached we can use our RNG function next integer between 1 and 8.
So a one out of eight chance.
And what we could do is we could get a track from our get random track function.
We'll play the track and then we'll wait for the track to complete.
Another function we're going to need is to listen to our game comms event, so we can call it on Game
comms event.
We'll get passed in action and arguments for that action.
And that means we're going to have to fill out our game comms enum on the server for our particular
service here.
So inside of server script service, inside of the modules and enum section, we can open this up and
we can create a key for our normal Squidward.
Service, and the only action that we want to have in here is basically just to move our Squidward to
a different position, because later on, again in our story, we're going to have our Squidward walking
along those invisible points when he goes and disappears into the basement and the player's got to go
look for him.
So that's the only action that we'll need for our normal Squidward service.
So that means inside of our on Gamescom event function, we can listen for if the action is equal to
the game comms enum server dot normal Squidward service dot move Squidward.
And if it is, then we can have a function for moving him.
So let's make that real quick.
We'll call this function Move Squidward to positions, and we'll pass the arguments from this function
to this function, and we can just call it right here and pass the args.
We don't need to fill this function out right now, but we will have to fill it out later.
Now we can go ahead and fill out the initialize and start functions for this service.
So normal.
Squidward service init.
And inside of this function, all we need to do is to connect our function to the game comms event.
So event connect on Gamescom event.
And then for our normal Squidward service Start function, we can call our, uh, start idle animation
and we can spawn this in a new thread using task dot defer.
That way we aren't stuck looping inside of this function forever.
And I'm pretty sure that's all we need to do for our Squidward service for now, so we can go ahead
and test out whether or not our Squidward has animations so we can actually just hit run here.
And the server initializes everything.
And there we go.
We have our Squidward Fran right here, all nice and animated.
Just chilling out.
Let's see if we can actually get another one of his idle animations to play here real quick.
And there we go.
Another one of his random idling animations has played.
So now we have further improved the ambience in our game, and so far our game is looking pretty good.
Keep it up and I will see you in the next lecture.